package com.stedu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Employee;
import com.stedu.bean.EmployeeVo;
import com.stedu.mapper.EmployeeMapper;
import com.stedu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageInfo findByPage(int currentPage, String ename, String ejob) {
        PageHelper.startPage(currentPage,5);
        List<EmployeeVo> employees= employeeMapper.findByPage(ename, ejob);
        PageInfo<EmployeeVo> pageInfo = new PageInfo<>(employees);
        return pageInfo;
    }

    @Override
    public List<Employee> findByDid(Integer did) {
        return employeeMapper.findByDid(did);
    }

    @Override
    public Employee findById(Integer eid) {
        return employeeMapper.findById(eid);
    }

    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void add(Employee employee) throws SQLException {
        //生成工号
        String maxEnoStr = employeeMapper.maxEno();
        int maxEno = Integer.parseInt(maxEnoStr);
        maxEno++;
        employee.setEno(maxEno + "");

        //添加
        employeeMapper.add(employee);

    }

    @Override
    public String maxEno() throws SQLException{
        return employeeMapper.maxEno();
    }

    @Override
    public int chgEstate(Integer eid, Integer estate) {
        return employeeMapper.chgEstate(eid,estate);
    }

    @Override
    public int update(Employee employee) {
        return employeeMapper.update(employee);
    }
}
